import 'dart:io';

import 'package:flutter/material.dart';
import 'package:jverify/jverify.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/constant/theme_colors.dart';

/// 极光认证
class JVerifyUtils {
  JVerifyUtils._internal();

  static Jverify _jv;

  static init() {
    if (_jv == null) {
      _jv = new Jverify();
    }
    // 打开调试模式
    _jv.setDebugMode(false);
    // 初始化sdk,  appKey 和 channel 只对ios设置有效
    _jv.setup(appKey: AppConfig.JVerification_key, channel: "devloper-default");

    _prepareLogin();
  }

  /// SDK是否初始化成功
  static Future<bool> isInitSuccess() async {
    Map<dynamic, dynamic> value = await _jv.isInitSuccess();
    bool result = value["result"];
    return result;
  }

  /// 预取号
  static Future<bool> _prepareLogin() async {
    Map<dynamic, dynamic> value = await _jv.preLogin();
    return true;
  }

  /// 一键登录认证
  static Future<bool> checkVerify() async {
    Map<dynamic, dynamic> value = await _jv.checkVerifyEnable();
    bool result = value["result"];
    debugPrint("一键登录:" + result.toString());
    if (result) {
      // 当前网络环境支持认证
      debugPrint("一键登录:当前网络环境支持认证");
    } else {
      // 当前网络环境不支持认证
      debugPrint("一键登录:当前网络环境不支持认证");
    }
    return result;
  }

  /// 一键登录授权
  static Future<Map<dynamic, dynamic>> loginAuth() async {
    if (!await isInitSuccess()) {
      init();
      return {"code": 0, "message": "SDK初始化失败", "operator": ""};
    }

    if (!await checkVerify()) {
      return {"code": 0, "message": "当前网络环境不支持认证", "operator": ""};
    }

    /*注意： ios在拉起授权页面之前 必须先 setCustomUI*/
    _setCustomUI();

    await _prepareLogin();

    /// 执行异步的一键登录接口
    Map<dynamic, dynamic> value = await _jv.loginAuth(true);

    debugPrint("一键登录:授权" + value.toString());
    return value;
  }

  static void _setCustomUI() {
    // final screenSize = MediaQuery.of(context).size;
    // final screenWidth = screenSize.width;
    // final screenHeight = screenSize.height;
    bool isiOS = Platform.isIOS;

    /// 自定义授权的 UI 界面，以下设置的图片必须添加到资源文件里，
    /// android项目将图片存放至drawable文件夹下，可使用图片选择器的文件名,例如：btn_login.xml,入参为"btn_login"。
    /// ios项目存放在 Assets.xcassets。
    ///
    JVUIConfig uiConfig = JVUIConfig();
    // uiConfig.authBackgroundImage = ;

    //uiConfig.navHidden = true;
    uiConfig.navColor = ThemeColors.colorWhite.value;
    uiConfig.navText = "登录";
    uiConfig.navTextColor = ThemeColors.colorType1.value;
    uiConfig.navReturnImgPath = "icon_close"; //图片必须存在

    uiConfig.logoWidth = 116;
    uiConfig.logoHeight = 116;
    // uiConfig.logoOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.logoWidth/2).toInt();
    uiConfig.logoOffsetY = 30;
    uiConfig.logoVerticalLayoutItem = JVIOSLayoutItem.ItemSuper;
    uiConfig.logoHidden = false;
    uiConfig.logoImgPath = "icon_login_logo";

    uiConfig.sloganOffsetY = isiOS ? 20 : 180;
    uiConfig.sloganVerticalLayoutItem = JVIOSLayoutItem.ItemNumber;
    uiConfig.sloganTextColor = ThemeColors.colorType7.value;
    uiConfig.sloganTextSize = 12;
    //        uiConfig.slogan
    // uiConfig.sloganHidden = true;

    uiConfig.numberFieldWidth = 200;
    uiConfig.numberFieldHeight = 40;
    //uiConfig.numFieldOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.numberFieldWidth/2).toInt();
    uiConfig.numFieldOffsetY = isiOS ? 20 : 200;
    uiConfig.numberVerticalLayoutItem = JVIOSLayoutItem.ItemLogo;
    uiConfig.numberColor = ThemeColors.colorType1.value;
    uiConfig.numberSize = 18;

    uiConfig.logBtnWidth = 300;
    uiConfig.logBtnHeight = 50;
    //uiConfig.logBtnOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.logBtnWidth/2).toInt();
    uiConfig.logBtnOffsetY = isiOS ? 20 : 300;
    uiConfig.logBtnVerticalLayoutItem = JVIOSLayoutItem.ItemSlogan;
    uiConfig.logBtnText = "本机号码一键登录";
    uiConfig.logBtnTextColor = ThemeColors.colorWhite.value;
    uiConfig.logBtnTextSize = 16;
    uiConfig.loginBtnNormalImage = "img_login_btn_bg"; //图片必须存在
    uiConfig.loginBtnPressedImage = "img_login_btn_bg"; //图片必须存在
    uiConfig.loginBtnUnableImage = "img_login_btn_bg"; //图片必须存在

    uiConfig.privacyState = false; //设置默认勾选
    uiConfig.privacyCheckboxSize = 15;
    uiConfig.checkedImgPath = "icon_login_privacy_box_select"; //图片必须存在
    uiConfig.uncheckedImgPath = "icon_login_privacy_box_normal"; //图片必须存在
    uiConfig.privacyCheckboxInCenter = false;
    //uiConfig.privacyCheckboxHidden = false;

    //uiConfig.privacyOffsetX = isiOS ? (20 + uiConfig.privacyCheckboxSize) : null;
    uiConfig.privacyOffsetY = 15; // 距离底部距离
    uiConfig.privacyVerticalLayoutItem = JVIOSLayoutItem.ItemSuper;
    uiConfig.clauseName = "《用户协议与隐私政策》";
    uiConfig.clauseUrl = ApiPath.url_privacy;
    uiConfig.clauseBaseColor = ThemeColors.colorType7.value;
    uiConfig.clauseColor = ThemeColors.colorType6.value;
    uiConfig.privacyText = ["我已阅读并同意", "和"];
    uiConfig.privacyTextSize = 13;
    uiConfig.privacyWithBookTitleMark = true;
    uiConfig.privacyTextCenterGravity = false;
    uiConfig.authStatusBarStyle = JVIOSBarStyle.StatusBarStyleDarkContent;
    uiConfig.privacyStatusBarStyle = JVIOSBarStyle.StatusBarStyleDefault;

    uiConfig.statusBarColorWithNav = true;
    uiConfig.virtualButtonTransparent = true;

    uiConfig.privacyStatusBarColorWithNav = false;
    uiConfig.privacyVirtualButtonTransparent = true;

    uiConfig.needStartAnim = true;
    uiConfig.needCloseAnim = true;

    uiConfig.privacyStatusBarColorWithNav = true;
    uiConfig.privacyNavColor = ThemeColors.colorWhite.value;
    uiConfig.privacyNavTitleTextColor = ThemeColors.colorType1.value;
    uiConfig.privacyNavTitleTextSize = 16;
    uiConfig.privacyNavTitleTitle1 = "用户服务与隐私协议";
    uiConfig.privacyNavTitleTitle2 = "用户服务与隐私政策";
    uiConfig.privacyNavReturnBtnImage = "return_bg"; //图片必须存在;

    /// 添加自定义的 控件 到授权界面
    List<JVCustomWidget> widgetList = [];

    /// 步骤 1：调用接口设置 UI
    _jv.setCustomAuthorizationView(true, uiConfig, landscapeConfig: uiConfig);
  }
}
